package com.ruoyi.community.controller.wxApi;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.community.domain.Housing;
import com.ruoyi.community.domain.vo.HousingVo;
import com.ruoyi.community.service.IHousingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 小区Controller
 *
 * @author WJ
 * @date 2022-02-22
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/wx/community/housing")
@Api(tags = {"小区"})
public class WxHousingController extends BaseController
{
    @Autowired
    private IHousingService housingService;

    /**
     * 查询小区列表
     */
    @ApiOperation(value = "查询小区列表")
    @GetMapping("/list")
    public TableDataInfo list(Housing housing)
    {
        startPage();
        List<HousingVo> list = housingService.selectHousingVoList(housing);
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId()==1){
                list.remove(i);
                break;
            }
        }
        if (ObjectUtils.isEmpty(list)){
            return getNoDataTable(list);
        }
        return getDataTable(list);
    }

    /**
     * 导出小区列表
     */
    @ApiOperation(value = "导出小区列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Housing housing)
    {
        List<Housing> list = housingService.selectHousingList(housing);
        ExcelUtil<Housing> util = new ExcelUtil<Housing>(Housing.class);
        util.exportExcel(response, list, "小区数据");
    }

    /**
     * 获取小区详细信息
     */
    @ApiOperation(value = "获取小区详细信息")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return AjaxResult.success(housingService.selectHousingById(id));
    }

    /**
     * 新增小区
     */
    @ApiOperation(value = "新增小区")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Housing housing)
    {
        return toAjax(housingService.insertHousing(housing));
    }

    /**
     * 修改小区
     */
    @ApiOperation(value = "修改小区")
    @PutMapping("edit")
    public AjaxResult edit(@RequestBody Housing housing)
    {
        if(housing.getId()==1){
            return AjaxResult.error("禁止修改此小区");
        }
        return toAjax(housingService.updateHousing(housing));
    }

    /**
     * 删除小区
     */
    @ApiOperation(value = "删除小区")
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        if(id==1){
           return AjaxResult.error("禁止删除此小区");
        }
        Long[] ids= {id};
        return toAjax(housingService.updateHousingByIds(ids));
    }
}
